import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/models/class/announcementDto.dart';
import 'package:ichinesereader/models/teacher/TeacherAssessment.dart';
import 'package:ichinesereader/utils/helper.dart';

// ignore: must_be_immutable
class UnfinishedStudentsDialog extends StatefulWidget {
  UnfinishedStudentsDialog(
      {Key? key, required this.studentList, required this.isHomework})
      : super(key: key);

  final List<StudentA> studentList;
  final bool isHomework;

  _UnfinishedStudentsDialog createState() => new _UnfinishedStudentsDialog();
}

class _UnfinishedStudentsDialog extends State<UnfinishedStudentsDialog> {
  final box = Hive.box('appconfig');
  final SchoolAdminController _controller = Get.find();
  bool isTeacher = false;

  @override
  void initState() {
    isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    super.initState();
  }

  _getCloseButton(context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0, 10, 10, 0),
      child: GestureDetector(
        onTap: () {},
        child: Container(
          alignment: FractionalOffset.topRight,
          child: GestureDetector(
            child: Icon(
              Icons.clear,
              color: Colors.red,
            ),
            onTap: () {
              Navigator.pop(context);
            },
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    var message = widget.isHomework
        ? 'Please complete your homework as early as possible!'
        : 'Please complete your assessment as early as possible!';

    return AlertDialog(
        title:
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          Text("Unfinshed students".tr),
          _getCloseButton(context),
        ]),
        titleTextStyle: TextStyle(
            fontWeight: FontWeight.bold,
            letterSpacing: 1,
            color: Get.isDarkMode ? Color(0xffcccccc) : Colors.black,
            fontSize: 20),
        backgroundColor: Get.isDarkMode
            ? (Color(0xff333333))
            : Color.fromARGB(255, 234, 241, 238),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(20))),
        content: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: Container(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: widget.studentList
                      .map((e) => Container(
                            decoration: BoxDecoration(
                              border: Border(
                                bottom: BorderSide(
                                  color: Color.fromARGB(255, 174, 213, 231),
                                  width: 1.0,
                                ),
                              ),
                            ),
                            padding: const EdgeInsets.all(10.0),
                            child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  IconButton(
                                    iconSize: 30,
                                    padding: EdgeInsets.zero,
                                    icon: Icon(
                                      Icons.notification_add,
                                      color: Colors.blueAccent,
                                    ),
                                    onPressed: () {
                                      if (isTeacher) {
                                        var classId = box.get(
                                            CacheManagerKey['currentclass']
                                                .toString());

                                        var dto = new ClassAnnouncementDto();
                                        dto.allStudentsClass = false;
                                        dto.message = message;
                                        dto.point = "0";
                                        dto.studentUserName = ['${e.userName}'];

                                        _controller
                                            .sendClassAnnouncementsByClassId(
                                                classId, dto)
                                            .then((result) {
                                          if (result.status == 1000) {
                                            showSnackMsg(
                                                'Message has been sent successfully.');
                                          } else {
                                            showSnackMsg('${result.message}');
                                          }
                                        });
                                      }
                                    },
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(e.firstName.toString()),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(e.lastName.toString()),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Text(e.userName.toString()),
                                ]),
                          ))
                      .toList())),
        ));
  }
}
